package com.schoolMall.service.impl;

import com.schoolMall.cache.RedisCache;
import com.schoolMall.dao.AreaDao;
import com.schoolMall.domain.Area;
import com.schoolMall.service.AreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:zyh
 * @Time:2021-08-26-21:45
 * @email:1269231889@qq.com
 */
@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaDao areaDao;
    @Autowired
    private RedisCache redisCache;
    private Logger logger = LoggerFactory.getLogger(AreaServiceImpl.class);
    private static final String AREA_KEY = "areaList";

    @Override
    public List<Area> getAreaList() {
        List<Area> areaList = null;
        try {
            if (redisCache.exists(AREA_KEY)){
                 areaList = (List<Area>) redisCache.get(AREA_KEY);
                 logger.info("redisCache get:{}",areaList);
            }else {
                 areaList = areaDao.queryArea();
                boolean hasSet = redisCache.set(AREA_KEY, areaList);
                if (!hasSet){
                    throw  new RuntimeException("设置缓存失败");
                }
            }
        }catch (Exception e){
            throw new RuntimeException("查询缓存出现问题");
        }
        return areaList;
    }
}
